package com.qf.group.common.base.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;



/**
 * @author 子未语
 */
public class CommonBeanutils extends BeanUtils {

    public static <S, T> T covertTo(S source, Supplier<T> targetSupplier) {
        if (ObjectUtils.isEmpty(source) || ObjectUtils.isEmpty(targetSupplier)) {
            return null;
        }
        T t = targetSupplier.get();
        copyProperties(source, t);
        return t;
    }


    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) {
        if (null == sources || null == targetSupplier) {
            return null;
        }
        return sources.stream().map(s -> covertTo(s, targetSupplier)).collect(Collectors.toList());
    }


    public static <S, T> IPage<T> convertPageInfo(IPage<S> source, IPage<T> target, Supplier<T> targetSupplier) {
        copyProperties(source, target);
        target.setRecords(convertListTo(source.getRecords(),targetSupplier));
        return target;
    }



}
